﻿using BaiTu.EventServices.Configuration;
using BaiTu.EventServices.Providers;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BaiTu.EventServices.Factory
{
    public static class RedisFactory
    {

        private static List<RedisSubscribeReceiver> RedisSubscribeReceivers = new List<RedisSubscribeReceiver>();
        private static object lockObj_RedisSubscribeReceivers = new object();

        public static RedisClient GetRedisClient_Publishable()
        {
            return BaiTu.CachingClientManager.RedisClientManager.RedisFactory.GetRedisClient();
        }

        public static RedisClient GetRedisClient_Subscribe()
        {
            return BaiTu.CachingClientManager.RedisClientManager.RedisFactory.GetRedisClient();
        }

        public static void TouchReceiveThread()
        {
            if (RedisSubscribeReceivers != null && RedisSubscribeReceivers.Count > 0)
                return;

            lock (lockObj_RedisSubscribeReceivers)
            {
                if (RedisSubscribeReceivers != null && RedisSubscribeReceivers.Count > 0)
                    return;

                //开启接收消息线程
                RedisSubscribeReceiver rsr = new RedisSubscribeReceiver(GetRedisClient_Subscribe(), Starter.eventResolver.Resolve);
                ThreadPool.QueueUserWorkItem(new WaitCallback(rsr.Start));
                Thread.Sleep(200);

                //将线程加入线程池
                RedisSubscribeReceivers.Add(rsr);
            }
        }
    }
}
